package com.zlm.website.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.util.PageUtils;
import com.zlm.website.service.entity.WebsiteNewsDO;
import com.zlm.website.service.service.WebsiteNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新闻控制器
 */
@RestController
@RequestMapping(value = "/news")
public class NewsController extends BaseController {

    @Autowired
    private WebsiteNewsService websiteNewsService;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 根据id获取新闻内容
     *
     * @param newsId
     * @return
     */
    @GetMapping(value = "/getNewsById/{newsId}")
    public JsonResultDto getNewById(@PathVariable String newsId) {
        return success();
    }

    /**
     * 根据新闻Id获取官网新闻
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getWebsiteNewsById/{id}")
    public JsonResultDto getWebsiteNewsById(@PathVariable Integer id) {
        WebsiteNewsDO websiteNewsDO = websiteNewsService.getEntityById(id);
        return success(websiteNewsDO);
    }

    /**
     * 根据类型获取任意类型新闻
     *
     * @param newsType
     * @return
     */
    @GetMapping(value = "/getWebsiteNewsByType/{newsType}")
    public JsonResultDto getWebsiteNewsByType(@PathVariable Integer newsType) {
        List<WebsiteNewsDO> newsList = websiteNewsService.getEntityByType(newsType);
        return success(newsList);
    }

    /**
     * 分页查询同一类型新闻，类型为空查询所有新闻
     *
     * @param newsType
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/queryAllWebsiteNews")
    @ResponseBody
    public JsonResultDto queryAllWebsiteNews(@RequestParam(name = "pageNumber", required = false) Integer pageNumber,
                                          @RequestParam(name = "pageSize", required = false) Integer pageSize,
                                          @RequestParam(name = "newsType", required = false) Integer newsType) {
        try {
            PageEntity page = websiteNewsService.queryAllWebsiteNews(newsType,pageNumber,pageSize);
            return success(page);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加官网新闻(此接口暂时只用于测试，请不要使用)
     * @param news
     * @return
     */
    @PostMapping("/insertWebsiteNews")
    @ResponseBody
    public JsonResultDto insertWebsiteNews(WebsiteNewsDO news){
        try {
            //测试用
            news.setCreateUser("admin");
            news.setLstUpdUser("admin");
            websiteNewsService.insertWebsiteNews(news);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取新闻播报（限定10条）
     * @return
     */
    @GetMapping("/getWebsiteNews")
    public JsonResultDto getWebsiteNews(){
        List<WebsiteNewsDO> newsList = websiteNewsService.getWebsiteNews();
        return success(newsList);
    }


}
